<?php
namespace Admin\Controller;
use Think\Controller;


class CouponController extends BaseCompanyController {
	public function index(){
		$this->display();
	}
	
	
	
  /**
     * 增加优惠券
     */
    public function couponadd(){
    	if(IS_POST){
    		$info=M("coupon");
    		
    		$num=I("post.num");
    		$id=$_SESSION['company']['id'];
    		for($i=0;$i<$num;$i++){
    			$string=randomkeys(12);
    			while ($info->where("value='".$string."'")->count()){
    			$string=randomkeys(12);
    		
    			}
    		$data=array(
    		"company_id"=>$id,
    		"value"=>$string,
    		"btime"=>strtotime($_POST['btime']." 00:00:00"),
    		"etime"=>strtotime($_POST['etime']." 00:00:00"),
    		"isused"=>0,
    		"context"=>$_POST['context']
    		);
    		$state=$info->add($data);
    		}
    		
    		
    		if( $state===false ){
                    $this->error('优惠券添加失败，错误信息：');
                }else{
                    $this->success('优惠券添加成功', U('company/couponlist',array('id'=>$id)), 0);
                }
    	}
    	else {
    	$this->assign("company_id",I("get.id"));
    	$this->display();
    	}
    
    }
/**
     * 增加优惠券
     */
    public function couponaddbycontext() {
    	if(IS_POST){
    		$info=M("coupon");
    	
    		$data=array(
    		"company_id"=>$_SESSION['company']['id'],
    		"value"=>$_POST['name'],
    		"btime"=>strtotime($_POST['btime']." 00:00:00"),
    		"etime"=>strtotime($_POST['etime']." 00:00:00"),
    		"isused"=>$_POST['isused'],
    		"used"=>0,
    		"context"=>$_POST['context']
    		);
    		$state=$info->add($data);
    		
    		
    		if( $state===false ){
                    $this->error('优惠券添加失败，错误信息：'.$info->getDbError());
                }else{
                    $this->success('优惠券添加成功', U('company/couponlist'), 0);
                }
    	}
    	else {
    	$this->assign("company_id",I("get.id"));
    	$this->display();
    	}
    
    }

    /* 优惠券删除 */
    public function coupondelete()
    {
    	$company_id=$_SESSION['company']['id'];
        if( is_array(I('id')) ){ //批量删除
            $id = implode( ',', I('id', array(), 'intval') );
        }else{ //单个删除
            $id = I('id/d');
        }

        $coupon = D('coupon');
	$state = $coupon->delete($id);
        if( $state===false ){
            $this->error('公司删除失败，错误信息: '.$coupon->getDbError()); //删除问卷，并关联删除旗下问题
        }else{
        	//var_dump(I('get.company_id'));
            redirect( U('coupon/couponlist',array('id'=>$company_id)));
        }
    }
    
    public function couponedit(){
    	if(IS_POST){
    	
    	$data=array(
    		"id"=>$_POST['id'],
    		"value"=>$_POST['name'],
    		"btime"=>strtotime($_POST['btime']." 00:00:00"),
    		"etime"=>strtotime($_POST['etime']." 00:00:00"),
    		"isused"=>$_POST['isused'],
    		"context"=>$_POST['context']
    		);
    	$info=M("coupon")->save($data);
    	if ($info==false){
    		$this->error('保存失败 ', U('coupon/couponlist'), 0); //删除问卷，并关联删除旗下问题
    	}else {
    		$this->success("保存成功" , U('coupon/couponlist'), 0);
    	}
    	
    	}
    	else{
    	$id=I('get.id');
    	$info=M("coupon")->where("id=".$id)->find();
    	
    	$this->assign("info",$info);
    	}
    	$this->display();
    	
    }
    
  /**
     * 公司优惠券列表
     */
    public function couponlist(){
    	
    	$data=M("coupon")->where("company_id=".$_SESSION['company']['id'])->select();
    	$this->assign("company_id",$_SESSION['company']['id']);
    	$this->assign("info",$data);
    	$this->display();
    
    }
 /**
     * 公司优惠券列表
     */
    public function seach(){
    	$name=$_GET["name"]==null?"":$_GET["name"];
    	if($name!="")
    	$data=M("coupon")->where("company_id=".$_SESSION['company']['id']." and value='".$name."'")->select();
    	else 
    	$data=M("coupon")->where("company_id=".$_SESSION['company']['id'])->select();
    	$this->assign("company_id",$_SESSION['company']['id']);
    	$this->assign("info",$data);
    	$this->display('couponlist');
    
    }
    
/**
	 * 登录
	 */
	public function login() {
		if(IS_POST)
		{
			$admin=M("company");
			$data['name']=$_POST['admin_name'];
			$data['pwd']=md5($_POST['admin_pwd']);
			
			
			if($this->check_verify($_POST['code'])){
			if($admin->where($data)->find())
			{
				$data=$admin->where($data)->find();
			
				session('company',$data);  
				redirect(U("coupon/index"));
			}
			else 
			{
				
				
			$this->error("账号密码错误");
			}
			}
			else {
			
				$this->error("验证码错误");
			}
			
		}
		else {
			$this->display();
		}
		
	}
	/**
	 * 注销
	 */
	public function logout(){
		session(null); // 清空当前的session
		redirect(U("index/index"));
	
	}
	/**
	 * 验证验证码
	 * @param 验证码
	 * @return boolean
	 */
	public function check_verify($code) {
		$verify = new \Think\Verify ();
		return $verify->check ( $code );
	}
	/**
	 * 生成验证码
	 */
	public function verify() {
		
		$Verify = new \Think\Verify ();
		$Verify->fontSize = 30;
		$Verify->length = 6;
		$Verify->useNoise = false;
		$Verify->fontttf = '5.ttf'; 
		$Verify->entry ();
	}
	
	public function companyinfo(){
	if(IS_POST){
    	if ($_FILES ['pic'] ['name'] != null)
         	$data ["pic"] = $this->upload ( $_FILES ['pic'] );
    	$data["id"]=$_SESSION['company']['id'];
    	$data["name"]=$_POST["name"];
    	if ($_POST["admin_pwd"]  != null)
    	$data["admin_pwd"]=md5($_POST["admin_pwd"]);
    		
    	$result=M("company")->save($data);
    	if($result){
				session('user',$data);  
    	$this->success("修改成功");
    	}
    	else
    	$this->error("修改失败");
    	}
    	else{
    	$data=M("company")->where("id=".$_SESSION['company']['id'])->find();
    	$this->assign("info",$data);
    	$this->display();
    	}
	}
	
 /**
	 * 上传
	 * @param unknown_type $filename
	 * @return string
	 */
	public function upload($filename) {
		$upload = new \Think\Upload (); // 实例化上传类
		$upload->maxSize = 3145728; // 设置附件上传大小
		$upload->exts = array ('jpg', 'gif', 'png', 'jpeg' ); // 设置附件上传类型
		$upload->rootPath = './Public/Upload/'; // 设置附件上传根目录
		$upload->savePath = '';
		// 上传单个文件 
		$info = $upload->uploadOne ( $filename );
		if (! $info) { // 上传错误提示错误信息
			$this->error ( $upload->getError () );
			exit ();
		} else { // 上传成功 获取上传文件信息
			return $info ['savepath'] . $info ['savename'];
		}
	}
	
}